package com.pixel.game.controls;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.pixel.game.entity.Unit;
import com.pixel.game.entity.UnitManager;
import com.pixel.game.Utils.PositionUtils;

public class InputHandler extends InputAdapter {
    private final UnitManager unitManager;
    private final Rectangle selectionRectangle;
    private Vector2 selectionStart;

    public InputHandler(UnitManager unitManager, Rectangle selectionRectangle) {
        this.unitManager = unitManager;
        this.selectionRectangle = selectionRectangle;
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        if (button == Input.Buttons.LEFT) {
            selectionStart = new Vector2(screenX, Gdx.graphics.getHeight() - screenY);
            selectionRectangle.set(selectionStart.x, selectionStart.y, 0, 0);
        }
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        if (selectionStart != null) {
            float x = Math.min(selectionStart.x, screenX);
            float y = Math.min(selectionStart.y, Gdx.graphics.getHeight() - screenY);
            float width = Math.abs(screenX - selectionStart.x);
            float height = Math.abs((Gdx.graphics.getHeight() - screenY) - selectionStart.y);
            selectionRectangle.set(x, y, width, height);
        }
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (button == Input.Buttons.LEFT) {
            selectUnitsInRectangle();//更新选中单位
            selectionStart = null;
        } else if (button == Input.Buttons.RIGHT) {
            Vector2 worldPosition = PositionUtils.screenToWorld(screenX, screenY);
            PositionUtils.arrangeUnitsInCompactFormation(unitManager.getSelectedUnits(), unitManager.getTargetPositions(), worldPosition, PositionUtils.getUnitSize());
        }
        return true;
    }

    /**
     * 更新选中单位
     */
    private void selectUnitsInRectangle() {
        unitManager.getSelectedUnits().clear();
        for (Unit unit : unitManager.getFriendlyUnits()) {
            if (selectionRectangle.contains(unit.getPosition())) {
                unitManager.getSelectedUnits().add(unit);
            }
        }
        if (!unitManager.getSelectedUnits().isEmpty()) {
            Vector2 center = PositionUtils.calculateCenter(unitManager.getSelectedUnits());
            PositionUtils.arrangeUnitsInCompactFormation(unitManager.getSelectedUnits(), unitManager.getTargetPositions(), center, PositionUtils.getUnitSize());
        }
    }

    public void drawSelectionRectangle(ShapeRenderer shapeRenderer) {
        if (selectionStart != null) {
            shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
            shapeRenderer.setColor(Color.WHITE);
            shapeRenderer.rect(selectionRectangle.x, selectionRectangle.y, selectionRectangle.width, selectionRectangle.height);
            shapeRenderer.end();
        }
    }
}
